/*
 * 
 */
package fr.univtln.superplanning;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityExistsException;
import javax.persistence.RollbackException;

import org.eclipse.persistence.exceptions.DatabaseException;

import fr.univtln.superplanning.api.actor.Administrator;
import fr.univtln.superplanning.api.actor.IPerson;
import fr.univtln.superplanning.api.actor.MainTeacher;
import fr.univtln.superplanning.api.actor.PersonType;
import fr.univtln.superplanning.api.actor.Student;
import fr.univtln.superplanning.api.actor.Teacher;
import fr.univtln.superplanning.api.event.EventType;
import fr.univtln.superplanning.api.resources.Building;
import fr.univtln.superplanning.api.resources.Classroom;
import fr.univtln.superplanning.api.study.Department;
import fr.univtln.superplanning.api.study.Promotion;
import fr.univtln.superplanning.api.study.Section;
import fr.univtln.superplanning.api.study.Step;
import fr.univtln.superplanning.api.study.Teaching;
import fr.univtln.superplanning.exceptions.NegativeNumberException;
import fr.univtln.superplanning.persistence.DBLogin;
import fr.univtln.superplanning.persistence.DatabaseManager;

public class TestConnectInsertion {

	/**
	 * @param args
	 */

	public static void main(String[] args) {
		DBLogin dblogin = DatabaseManager.getDbLogin();

		DBLogin connect = DatabaseManager.getDbLogin();
		connect.setHost("localhost");
		connect.setDatabaseName("Superplanning");
		connect.setUserName("***");

		try {
			connect.setPort(5432);
		} catch (NegativeNumberException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		connect.setPassword("***");
		connect.setProtocole("postgresql");
		try {
			connect.login();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// RightsAccessLevel admin = new RightsAccessLevel();
		/*
		 * s1 = new Student("BOURDEAU", "Romain", "rowmain@gmail.com", "romain",
		 * "123",
		 * 636353,"3654464",null,RightsAccessLevel.Level.ADMIN_APPLICATION_RIGHTS
		 * );
		 */

		List<IPerson> persons = new ArrayList<IPerson>();
		List<PersonType> personTypes = new ArrayList<PersonType>();
		List<Teaching> teachings = new ArrayList<Teaching>();
		List<Step> steps = new ArrayList<Step>();
		List<Department> departments = new ArrayList<Department>();
		List<Section> sections = new ArrayList<Section>();
		List<Building> buildings = new ArrayList<Building>();
		List<Classroom> classrooms = new ArrayList<Classroom>();
		List<Promotion> promotions = new ArrayList<Promotion>();
		List<EventType> eventTypes = new ArrayList<EventType>();

		List<Object> persistentObjects = new ArrayList<Object>();

		// Create some person types

		PersonType mcfPersonType = new PersonType("Maître de conférence", "MCF");
		PersonType prPersonType = new PersonType("Professeur des universités", "PR");
		PersonType docPersonType = new PersonType("Doctorant", "DOC");
		PersonType etudiantPersonType = new PersonType("Etudiant", "Etud");
		PersonType adminPersonType = new PersonType("Adminitrateur", "ADM");

		personTypes.add(mcfPersonType);
		personTypes.add(prPersonType);
		personTypes.add(docPersonType);
		personTypes.add(etudiantPersonType);
		personTypes.add(adminPersonType);

		// RightsAccessLevel studentRights = new RightsAccessLevel(
		// RightsAccessLevel.Level.STUDENT_RIGHTS, "enseignement");
		// RightsAccessLevel adminRights = new RightsAccessLevel(
		// RightsAccessLevel.Level.ADMIN_APPLICATION_RIGHTS, "administrateur");
		// RightsAccessLevel teacherLevel = new RightsAccessLevel(
		// RightsAccessLevel.Level.TEACHER_RIGHTS, "enseignant");

		Student vincent = new Student("Vincent", "M", "vincent***@etud.univtln.fr", "vincentm",
				"tititoto", 123456781, "4887554", etudiantPersonType, 5);
		Student romain = new Student("Romain", "B", "romain***@etud.univtln.fr", "romainb",
				"tititoto", 123456782, "4887555", etudiantPersonType, 5);
		Student jon = new Student("Jonathan", "G", "jon***@etud.univtln.fr", "jonathang",
				"tititoto", 123456783, "4887556", etudiantPersonType, 5);

		Administrator jmp = new Administrator("Jean M", "P", "jmp@univ-tln.fr", "jmp",
				"securepassword", adminPersonType, 9);

		Teacher durant = new Teacher("Durant", "Hervé", "durant@univ-tln.fr", "durant", "titi",
				mcfPersonType, 4, teachings);
		MainTeacher ocsasirum = new MainTeacher("Ocsasirum", "El", "ocsas@univ-tln.fr", "ocsas",
				"titi", prPersonType, 5, teachings);
		Teacher onurb = new Teacher("Onurb", "Em", "onurb@univ-tln.fr", "onurb", "titi",
				mcfPersonType, 4, teachings);
		Teacher kizar = new MainTeacher("Kizar", "Jo", "kizar@univ-tln.fr", "kizard", "tititoto",
				prPersonType, 4, teachings);

		// ADD ALL PERSONS
		persons.add(vincent);
		persons.add(romain);
		persons.add(jon);
		persons.add(jmp);
		persons.add(durant);
		persons.add(ocsasirum);
		persons.add(onurb);
		persons.add(kizar);

		Teaching inf5 = new Teaching("Théorie de l'info. codage", "INF5", "codage en dur");
		Teaching inf10 = new Teaching("Documents XML", "INF10", "XSLFO, Maven et XML");
		Teaching inf8 = new Teaching("Base de données avancées", "INF8",
				"Médiation, Trigger, données multi dimentionnelles");
		Teaching inf9 = new Teaching("Conception d'un système d'information", "INF9", "UML");

		// Add all teachings
		teachings.add(inf5);
		teachings.add(inf10);
		teachings.add(inf8);
		teachings.add(inf9);

		Step m1 = new Step("Master 1", "M1", "Master 1° année");
		Step l3 = new Step("Licence 3", "L3", "Licence 3° année");

		// add all steps
		steps.add(m1);
		steps.add(l3);

		Department sciencesEtTech = new Department("Sciences et Techniques", "Stech", "");
		departments.add(sciencesEtTech);

		Section infoSection = new Section("Informatique", "Info", "Filière informatique",
				sciencesEtTech);
		sections.add(infoSection);

		Promotion l3info = new Promotion("L3 INFO Année 2010-2011", "L3-INFO-2011", 2011, l3,
				ocsasirum);
		Promotion m1sis = new Promotion("M1 SIS Année 2010-2011", "M1-SIS-2011", 2011, m1,
				ocsasirum);
		promotions.add(l3info);
		promotions.add(m1sis);

		// Add buildings and classrooms
		Building buildingR = new Building("U_prime", "Bâtiment U' - S&T", "Salles équipées");
		buildings.add(buildingR);

		Classroom uprime001 = new Classroom("U_prime_001", 20);
		Classroom uprime002 = new Classroom("U_prime_002", 20);
		Classroom uprime003 = new Classroom("U_prime_003", 30);

		buildingR.add(uprime001);
		buildingR.add(uprime002);
		buildingR.add(uprime003);

		classrooms.add(uprime001);
		classrooms.add(uprime002);
		classrooms.add(uprime003);

		// Add event type
		EventType typeCM = new EventType("CM", "Cours magistrale");
		EventType typeTD = new EventType("TD", "Travaux dirigés");
		EventType typeTP = new EventType("TP", "Travaux pratiques");
		eventTypes.add(typeTP);
		eventTypes.add(typeTD);
		eventTypes.add(typeCM);

		// Create the persist object list
		for (IPerson person : persons)
			persistentObjects.add(person);
		for (PersonType personType : personTypes)
			persistentObjects.add(personType);
		for (Teaching teaching : teachings)
			persistentObjects.add(teaching);
		for (Step step : steps)
			persistentObjects.add(step);
		for (Department department : departments)
			persistentObjects.add(department);
		for (Section section : sections)
			persistentObjects.add(section);
		for (Building building : buildings)
			persistentObjects.add(building);
		for (Classroom classroom : classrooms)
			persistentObjects.add(classroom);
		for (Promotion promotion : promotions)
			persistentObjects.add(promotion);
		for (EventType eventType : eventTypes)
			persistentObjects.add(eventType);

		try {

			DatabaseManager.getDbLogin().getEntityManager().getTransaction().begin();

			for (Object persistentObject : persistentObjects)
				DatabaseManager.getDbLogin().getEntityManager().persist(persistentObject);

			DatabaseManager.getDbLogin().getEntityManager().getTransaction().commit();

		} catch (EntityExistsException e) {
			System.err.println("entity already exists");
		} catch (DatabaseException e) {
			System.err.println("database exception");
		} catch (RollbackException e) {
			System.err.println("rollback exception (maybe entity already exists)");

		}
	}
}
